package cn.itcast.dao.impl;

import cn.itcast.dao.refactor.AbstractDaoOptimization;
import cn.itcast.dao.RowMapper;
import cn.itcast.po.User;

import java.sql.ResultSet;
import java.sql.SQLException;

/*
* (策略者模式)
* 组合优化JDBC: 方法参数增加实现RowMapper接口的匿名对象
*
* 这里UserDaoImplOptimization有点装饰类的意思
* */
public class UserDaoImplOptimization {

    private AbstractDaoOptimization abstractDaoOptimization = new AbstractDaoOptimization();

    public void updateUser(User user) {
        String sql = "UPDATE user SET address=? WHERE id=?";
        Object[] obj = new Object[]{user.getAddress(), user.getId()};
        abstractDaoOptimization.updateUser(sql, obj);
    }

    public User findUser(int id){
        String sql = "select * from user where id=?";
        return (User) abstractDaoOptimization.findUser(sql, id, new RowMapper() {
            @Override
            public Object rowMapper(ResultSet rs) throws SQLException {
                User user = new User();
                user.setName(rs.getString("username"));
                user.setDate(rs.getDate("birthday"));
                user.setSex(rs.getString("sex"));
                user.setAddress(rs.getString("address"));
                return user;
            }
        });
    }
}
